package com.hongxin.nw.netty.model.analysis;

import com.hongxin.nw.netty.utils.BinaryTools;
import com.hongxin.nw.netty.utils.FieldAnalysis;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @remark
 * @author: GuoXiaolin
 * @date 2019/5/17 15:48
 * @Version 1.0
 * @Descibe
 */
@Slf4j
@Getter
@Setter
public class ASDU {

    private int sq;  //  0,1
    private int gun;//枪号
    private int type;//在7.2.1中定义	类型标识（TYPE）	数据单元 标识符
    private int vsq;//在7.2.2中定义	可变结构限定词（VSQ）
    private int cot;//在7.2.3中定义	传送原因（COT）
    private String staadr;//在7.2.4中定义	应用服务数据单元公共地址
    private String infoAdr;//在7.2.5中定义   信息对象地址

    private ByteBuf byteBuf;
    private int idx = 0;

    private void subType() {
//        this.type = FieldAnalysis.analysisBin(substring(TypeIdentif.BYTE_TYPE.getByteLength()));

        int bytelength = TypeIdentif.BYTE_TYPE.getInt();
        String var = BinaryTools.subByteBuf(bytelength, idx, byteBuf);
        this.type = Integer.parseInt(FieldAnalysis.analysisBin(var));
        this.idx += bytelength;
    }
    private void subVsq() {
//        this.vsq = substring(TypeIdentif.BYTE_VSQ.getByteLength());
//        this.vsq = Integer.toBinaryString(Integer.parseInt(this.vsq, 16));
//        this.vsq = BinaryTools.addZero(this.vsq, 8, StaticValue.DIRECTION_LEFT);
//        this.sq = Integer.parseInt(String.valueOf(this.vsq.charAt(0)));
//        this.vsq  = String.valueOf(Integer.parseInt(this.vsq.substring(1), 2));//一共有几个信息对象

        int bytelength = TypeIdentif.BYTE_VSQ.getInt();
        String var = BinaryTools.subByteBuf(bytelength, idx, byteBuf);
        var = Integer.toBinaryString(Integer.parseInt(var, 16));
        var = BinaryTools.addZero(var, 8, StaticValue.DIRECTION_LEFT);
        this.sq = Integer.parseInt(String.valueOf(var.charAt(0)));
        this.vsq  = Integer.parseInt(var.substring(1), 2);//一共有几个信息对象
        this.idx += bytelength;
    }
    private void subCot() {
//        this.cot = substring(TypeIdentif.BYTE_COT.getByteLength());
//        this.cot = FieldAnalysis.toOct(this.cot);

        int bytelength = TypeIdentif.BYTE_COT.getInt();
        String var = BinaryTools.subByteBuf(bytelength, idx, byteBuf);
        this.cot = Integer.parseInt(FieldAnalysis.toOct(var));
        this.idx += bytelength;
    }
    private void subStaadr() {
//        this.staadr = substring(TypeIdentif.BYTE_STAT_ADDRESS.getByteLength());
//        this.staadr = FieldAnalysis.toOct(this.staadr);

        int bytelength = TypeIdentif.BYTE_STAT_ADDRESS.getInt();
        this.staadr = BinaryTools.subByteBuf(bytelength, idx, byteBuf);
        this.staadr = FieldAnalysis.toOct(this.staadr);
        this.idx += bytelength;
    }
    private void subInfoAdr(){
        if (TypeIdentif.IDENTIFY_M_JC_NA_1.getInt() == type || TypeIdentif.IDENTIFY_M_RE_NA_1.getInt() == type){
            if (StaticValue.SQ0 == this.sq){
//                this.infoadr = substring(TypeIdentif.BYTE_INFO_ADDRESS.getByteLength());
//                this.gun = String.valueOf(this.infoadr.charAt(0));
//                this.infoadr = FieldAnalysis.analysisBin(this.infoadr);

                int bytelength = TypeIdentif.BYTE_INFO_ADDRESS.getInt();
                this.infoAdr = BinaryTools.subByteBuf(bytelength, idx, byteBuf);
//                this.gun = Integer.parseInt(this.infoAdr.substring(0,1));
//                this.infoAdr = FieldAnalysis.analysisBin(this.infoAdr);

                this.infoAdr = BinaryTools.converter(this.infoAdr);
                this.gun = Integer.parseInt(this.infoAdr.substring(0,1));
                this.infoAdr = String.valueOf(Integer.parseInt(this.infoAdr.substring(2),16));
                this.idx += bytelength;
            }
        }else{
            if (StaticValue.SQ1 == this.sq){
//                this.infoadr = substring(TypeIdentif.BYTE_INFO_ADDRESS.getByteLength());
//                this.gun = String.valueOf(this.infoadr.charAt(0));
//                this.infoadr = FieldAnalysis.analysisBin(this.infoadr);

                int bytelength = TypeIdentif.BYTE_INFO_ADDRESS.getInt();
                this.infoAdr = BinaryTools.subByteBuf(bytelength, idx, byteBuf);
//                this.gun = Integer.parseInt(this.infoAdr.substring(0,1));
//                this.infoAdr = FieldAnalysis.analysisBin(this.infoAdr);

                this.infoAdr = BinaryTools.converter(this.infoAdr);
                this.gun = Integer.parseInt(this.infoAdr.substring(0,1));
                this.infoAdr = String.valueOf(Integer.parseInt(this.infoAdr.substring(2),16));
                this.idx += bytelength;
            }
        }
    }

    public int analysisStr(int idx, ByteBuf byteBuf){
        this.idx = idx;
        this.byteBuf = byteBuf;
        subType();
        subVsq();
        subCot();
        subStaadr();
        subInfoAdr();
        return this.idx;
    }

    @Override
    public String toString() {
        return "ASDU{" +
                "type=" + type +
                ", vsq=" + vsq +
                ", cot=" + cot +
                ", staadr='" + staadr + '\'' +
                ", infoAdr='" + infoAdr + '\'' +
                ", gun='" + gun + '\'' +
                ", sq=" + sq +
                '}';
    }

}
